<template>
    <div class="connect">
        <Login ref="login"></Login>
    </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
    name: "appConnect",
    layout: "layout-empty",
    data () {
        return {
            token_key: "token_key",
            shop: "",
            wix_instance_id: "",
        };
    },
    computed: {
        ...mapGetters({
            isLogin: "getToken",
        }),
    },
    methods: {
        // 连接店铺shopify
        async connectShopifyStore () {
            let shop_name = this.shop;
            let res = await this.$http("AppStore.putAssociatedShop", {
                shop_name,
            });
            if (res.code == 0) {
                this.$gmeRepore('addToStore');
                this.$uxMessage.success("Shop added successfully");
                //暂时先都跳转到用户仪表盘
                this.$router.push("/user");
                // this.$router.push({ path: "/", query: { shopify: shop_name } });
            } else {
                this.$router.replace("/");
                this.$uxMessage.warning('Failed to add shop');
            }
        },
        // shopify点击打开我们网站页面，要跳转到网站连接shopify的页面
        async getTokenByKey () {
            // let shop_name = this.shop;
            let res = await this.$http("AppStore.getTokenByKey", {
                token_key: this.token_key,
            });
            if (res.code == 0) {
                this.$store.dispatch("doLogin", res.data.token).then(() => {
                    //暂时先都跳转到用户仪表盘
                    this.$router.push("/user");
                })

                // this.$router.push("/user/store");
                // this.$router.push({ path: "/", query: { shopify: shop_name } });
            } else {
                this.$uxMessage.warning(res.msg);
                this.$router.replace("/");
            }
        },
        // 连接店铺Wix
        async connectWixStore () {
            let wix_instance_id = this.wix_instance_id;
            let res = await this.$http("AppStore.putWixBind", {
                wix_instance_id,
            });
            if (res.code == 0) {
                this.$gmeRepore('addToStore');
                this.$uxMessage.success("Shop added successfully");

                //暂时先都跳转到用户仪表盘
                this.$router.push("/user");
            } else {
                this.$router.replace("/");
                this.$uxMessage.warning('Failed to add shop');
            }
        },
        formatQuery () {
            // 初始化数据
            let query = this.$route.query || {};
            this.$store.dispatch("doLogout");
            if (query.shop_name) {
                this.shop = query.shop_name || "";
            } else if (query.key) {
                this.shop = query.key.replace("token_key_", "");
            }
            this.token_key = query.key || "";
            this.wix_instance_id = query.wix_instance_id || "";
        },
        judgeStore () {
            //判断商店类型
            if (this.wix_instance_id != '') {
                //wix
                this.$emit("global:userLogin", () => this.connectWixStore());
            } else if (this.token_key != '' || this.shop != '') {
                //shopify
                if (this.token_key != '') {
                    //token_key 跳转
                    this.getTokenByKey();
                } else {
                    // 绑定店铺
                    this.$emit("global:userLogin", () => this.connectShopifyStore());
                }
            } else {
                this.$router.replace("/");
            }
        }
    },
    created () {
        if (process.client) {
            // let query = this.$route.query || {};
            // this.$store.dispatch("setToken", "");
            // if (query.shop_name) {
            //     this.shop = this.$route.query.shop_name;
            // } else if (query.key) {
            //     this.shop = this.$route.query.key.replace("token_key_", "");
            // }
            // this.token_key = query.key || "";
            this.formatQuery()
        }
    },
    mounted () {
        // if (this.token_key != "") {
        //     this.getTokenByKey();
        // } else {
        //     // let dom = this.$refs.login;
        //     // dom.show = true;
        //     // dom.status = 2;
        //     if (this.shop != "") {
        //         this.$emit("global:userLogin", () => {
        //             this.connectShopifyStore();
        //         });
        //     } else {
        //         this.$router.replace("/");
        //     }
        // }
        this.judgeStore()
    },
};
</script>
<style lang="scss" scoped>
::v-deep {
    .login-close {
        display: none;
    }
}
</style>
